﻿Public Class ChiTietSuDungTheForm
    Dim dt As DataTable    
    Dim obj As ChiTietSuDungThe
    Dim MaTheTinDung As String
    Sub New(ByVal ma_the As String, ByRef dt As DataTable, Optional ByVal obj As ChiTietSuDungThe = Nothing)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        MaTheTinDung = ma_the
        Me.dt = dt
        txtMaTheTinDung.Text = MaTheTinDung
        If obj Is Nothing Then
            btThem.Visible = True
            btSua.Visible = False
        Else
            Me.obj = obj
            btThem.Visible = False
            btSua.Visible = True
            txtChiTiet.Text = obj.ChiTiet
            txtSoTien.Text = obj.SoTien
            dtNgaySuDung.Text = obj.NgaySuDung
        End If
    End Sub
    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        Me.Dispose()
    End Sub

    Private Sub btThem_Click(sender As System.Object, e As System.EventArgs) Handles btThem.Click
        If txtSoTien.Text.Trim().Length = 0 Or Not IsNumeric(txtSoTien.Text) Then
            MessageBox.Show("Xin vui lòng nhập vào số tiền")
            Return
        End If
        If txtChiTiet.Text.Trim().Length = 0 Then
            MessageBox.Show("Xin vui lòng nhập vào chi tiết")
            Return
        End If        
        Dim dr As DataRow = dt.NewRow()
        dr("MaTheTinDung") = MaTheTinDung
        dr("SoTien") = Double.Parse(txtSoTien.Text)
        dr("NgaySuDung") = dtNgaySuDung.Text
        dr("ChiTiet") = txtChiTiet.Text
        dt.Rows.Add(dr)
        DB_Provider.Write(dt, "ChiTietSuDungThe")
        Me.Dispose()
    End Sub

    Private Sub btSua_Click(sender As System.Object, e As System.EventArgs) Handles btSua.Click        
        Dim dr As DataRow = dt.Rows.Find(obj.ID)
        dr.BeginEdit()
        dr("SoTien") = Double.Parse(txtSoTien.Text)
        dr("NgaySuDung") = dtNgaySuDung.Text
        dr("ChiTiet") = txtChiTiet.Text
        dr.EndEdit()
        DB_Provider.Write(dt, "ChiTietSuDungThe")
        Me.Dispose()
    End Sub
End Class